package com.suning.sawp.member.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.customer.CustshopFrequency;
import com.suning.sawp.dto.esb.CustSocietyInfoStruct;
import com.suning.sawp.dto.esb.LocateMemberInfo;
import com.suning.sawp.dto.esb.QuerySocialityInfoResp;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.esb.MemberInfoService;
import com.suning.sawp.intf.esb.ShopFrequencyService;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.dao.MemCustomerDao;
import com.suning.sawp.member.dto.CustomerDetail;
import com.suning.sawp.member.dto.MemCustomer;
import com.suning.sawp.member.dto.MemCustomerIntentCate;
import com.suning.sawp.member.dto.MemCustomerIntentCmmdty;
import com.suning.sawp.member.dto.MemFollowLog;
import com.suning.sawp.member.service.LabelService;
import com.suning.sawp.member.service.MemCustomerService;
import com.suning.sawp.member.vo.CustLabelVo;
import com.suning.sawp.member.vo.MemCustomerDetailResponse;
import com.suning.sawp.member.vo.MemCustomerIntentCateVo;
import com.suning.sawp.member.vo.MemCustomerIntentCmmdtyVo;
import com.suning.sawp.member.vo.MemCustomerResponse;
import com.suning.sawp.member.vo.MemCustomerVo;
import com.suning.sawp.member.vo.MemFollowLogResponse;
import com.suning.sawp.member.vo.MemFollowLogVo;
import com.suning.sawp.service.impl.companymem.KafkaProducer;
import com.suning.sawp.service.util.AESUtils;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.service.util.DateUtils;

/**
 * 我的客户接口实现 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service(value = "memCustomerService")
public class MemCustomerServiceImpl implements MemCustomerService {

	/**
	 * 日志
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(MemCustomerServiceImpl.class);

	/**
	 * 客户数据库服务
	 */
	@Autowired
	MemCustomerDao memCustomerDao;

	/**
	 * 客户购物状态服务
	 */
	@Autowired
	ShopFrequencyService shopFrequencyService;

	/**
	 * 员工信息服务
	 */
	@Autowired
	StoreManService storeManService;

	@Autowired
	private LabelService labelService;

	@Autowired
	private MemberInfoService memberInfoService;

	@Value("#{settingConfig[cm_kafkaZkServer]}")
	String kafkaZkServer;

	@Value("#{settingConfig[cm_topic]}")
	String topic;

	public ReturnMsg<JSONObject> index(String staffId) {
		LOGGER.info("会员首页接口查询，登录人：{}", staffId);
		ReturnMsg<JSONObject> returnMsg = new ReturnMsg<JSONObject>();
		// 查询我的客户数量
		int myCustomerNum = memCustomerDao.countMyCustomer(staffId);
		// 查询今日待跟进客户数量
		int todayFollowNum = memCustomerDao.countTodayWillFollowCustom(staffId);
		JSONObject json = new JSONObject();
		json.put("myCustomerNum", myCustomerNum);
		json.put("todayFollowNum", todayFollowNum);
		returnMsg.setData(json);
		return returnMsg;
	}

	/**
	 * 查询客户列表
	 */
	@Override
	public ReturnMsg<MemCustomerResponse> queryMyCustomers(String storeCode, String staffId, String custName, String buyDateType, boolean isToday, int currIndex, int size) {
		LOGGER.info("queryMyCustomers,{},{}{},{}{},{}", storeCode, staffId, custName, buyDateType, isToday, currIndex);
		ReturnMsg<MemCustomerResponse> returMsg = new ReturnMsg<MemCustomerResponse>();
		List<MemCustomer> list = memCustomerDao.queryMyCustomers(storeCode, staffId, custName, buyDateType, isToday, currIndex, size);
		// 数据不为空时
		if (CollectionUtils.isNotEmpty(list)) {
			MemCustomerResponse resp = new MemCustomerResponse();
			List<MemCustomerVo> custs = convertMemCustomers(list);
			resp.setList(custs);
			returMsg.setData(resp);
		}
		LOGGER.info("queryMyCustomers,resp={}", returMsg);
		return returMsg;
	}

	/**
	 * 转换客户数据 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param list
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private List<MemCustomerVo> convertMemCustomers(List<MemCustomer> list) {
		List<MemCustomerVo> custs = new ArrayList<MemCustomerVo>();
		MemCustomerVo vo;
		List<String> custList = new ArrayList<String>();
		// 处理客户编码,方便后面查询客户购物状态
		for (MemCustomer cust : list) {
			if (null != cust && StringUtils.isNotBlank(cust.getCustNo())) {
				custList.add(cust.getCustNo());
			}
		}
		// 查询会员首购复购接口
		Map<String, String> frequencyList = shopFrequencyService.queryShopFrequency(custList);
		// 处理每个客户信息
		for (MemCustomer cust : list) {
			// 数据不为空
			if (null != cust) {
				vo = new MemCustomerVo();
				vo.setCustId(cust.getId());
				vo.setCustName(StringUtils.isBlank(cust.getName()) ? "顾客" : cust.getName());

				// 加密手机号
				if (StringUtils.isNotBlank(cust.getMobile())) {
					vo.setMobile(AESUtils.encrypt(cust.getMobile(), ConfigConstants.AES_KEY));
				} else {
					vo.setMobile("");
				}
				// 预计购买金额
				if (null != cust.getExpectPay()) {
					vo.setExpectPay(cust.getExpectPay().toString());
				} else {
					vo.setExpectPay("0.00");
				}
				// 日期转换
				if (null != cust.getExpectDate()) {
					vo.setExpectDate(DateUtils.getDateStrByFormat(cust.getExpectDate(), "yyyy-MM-dd"));
				} else {
					vo.setExpectDate("");
				}
				if (null != cust.getCreateTime()) {
					vo.setCreateTime(DateUtils.getDateStrByFormat(cust.getCreateTime(), "yyyy-MM-dd"));
				} else {
					vo.setCreateTime("");
				}
				// 设置首购，复购，未购提示信息
				vo.setBuyStatus(processBuyStatus(cust.getCustNo(), frequencyList));

				// 查询标签列表
				List<CustLabelVo> custLabels = labelService.queryFiveLabelByCustId(cust.getId());
				vo.setCustLabels(custLabels);

				custs.add(vo);
			}
		}
		return custs;
	}

	/**
	 * 查询客户详情
	 */
	@Override
	public ReturnMsg<MemCustomerDetailResponse> queryMyCustomerDetail(Long custId) {
		LOGGER.info("queryMyCustomerDetail,{}", custId);
		ReturnMsg<MemCustomerDetailResponse> returnMsg = new ReturnMsg<MemCustomerDetailResponse>();
		MemCustomer customer = memCustomerDao.getMemCustomer(custId);
		if (null != customer) {
			MemCustomerDetailResponse resp = new MemCustomerDetailResponse();
			resp.setCustId(custId);
			resp.setDetailId(customer.getMemberDetailId());
			resp.setCustName(StringUtils.isBlank(customer.getName()) ? "顾客" : customer.getName());

			String custNo = customer.getCustNo();
			if (StringUtils.isEmpty(custNo) && StringUtils.isNotEmpty(customer.getMobile())) { // 没有会员编码的时候，根据手机号查询会员编码
				LocateMemberInfo memberInfo = memberInfoService.locateMemberByMobileNo(customer.getMobile());
				custNo = memberInfo.getCustNum();
			}
			List<String> custList = new ArrayList<String>();
			// 如果查到了，是会员，则设置会员编码，如果不是会员，则略过
			if (StringUtils.isNotEmpty(custNo)) { // 每次查询详情的时候，都实时查等级
				resp.setCustNo(custNo);
				CustSocietyInfoStruct info = queryMemberInfoByCustNo(custNo);
				String level = customer.getLevel();
				if (info != null && StringUtils.isNotEmpty(info.getCustLevelNum())) {
					level = MemberConstants.CUST_LEVEL.get(info.getCustLevelNum());
				}
				if (StringUtils.isNotEmpty(level)) {
					level = "V0"; // 各种方式都没查到之后，等级为V0
				}
				resp.setLevel(level);
				custList.add(customer.getCustNo());
			}

			// 查询会员首购复购接口
			Map<String, String> frequencyList = shopFrequencyService.queryShopFrequency(custList);
			resp.setBuyStatus(processBuyStatus(customer.getCustNo(), frequencyList));

			// 加密手机号
			if (StringUtils.isNotBlank(customer.getMobile())) {
				resp.setMobile(AESUtils.encrypt(customer.getMobile(), ConfigConstants.AES_KEY));
			} else {
				resp.setMobile("");
			}
			// 日期转换
			if (null != customer.getExpectDate()) {
				resp.setExpectDate(DateUtils.getDateStrByFormat(customer.getExpectDate(), "yyyy-MM-dd"));
			} else {
				resp.setExpectDate("");
			}
			// 预计购买金额
			if (null != customer.getExpectPay()) {
				resp.setExpectPay(customer.getExpectPay().toString());
			} else {
				resp.setExpectPay("0.00");
			}
			// 详情ID不为空
			if (null != customer.getMemberDetailId() && customer.getMemberDetailId().longValue() > 0) {
				// 查询客户详情信息
				CustomerDetail detail = memCustomerDao.getMemCustomerDetail(customer.getMemberDetailId());
				if (null != detail) {
					resp.setMode(String.valueOf(detail.getMode()));
					resp.setSource(String.valueOf(detail.getSource()));
					resp.setActivityName(detail.getActivityName());
					resp.setActivityId(detail.getActivityId());
				}
				// 查询客户意向品类品牌
				List<MemCustomerIntentCate> cates = memCustomerDao.queryMemCustomerIntentCates(customer.getMemberDetailId());
				resp.setCates(convertMemCustomerIntentCates(cates));
				// 查询客户意向商品
				List<MemCustomerIntentCmmdty> cmmdtys = memCustomerDao.queryMemCustomerIntentCmmdtys(customer.getMemberDetailId());
				resp.setCommoditys(convertMemCustomerIntentCmmdtys(cmmdtys));

				// 查询标签信息
				List<CustLabelVo> custLabels = labelService.queryLabelByCust(String.valueOf(custId));
				resp.setCustLabels(custLabels);

			}
			returnMsg.setData(resp);
		} else {
			returnMsg.setError(ErrorCodeConstants.ERR_MEM_0022);
		}
		LOGGER.info("queryMyCustomerDetail,response={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 根据会员编码查询会员相关信息
	 * 
	 * @param custNo
	 * @return
	 */
	private CustSocietyInfoStruct queryMemberInfoByCustNo(String custNo) {
		if (StringUtils.isEmpty(custNo)) {
			return null;
		}
		List<String> custNumList = new ArrayList<String>();
		custNumList.add(custNo);
		QuerySocialityInfoResp infoResp = memberInfoService.querySocialityInfoAConstellation(custNumList);
		if (null != infoResp && null != infoResp.getBody()) {
			if (CollectionUtils.isNotEmpty(infoResp.getBody().getCustSocietyInfoStructList())) {
				CustSocietyInfoStruct infoStruct = infoResp.getBody().getCustSocietyInfoStructList().get(0);
				return infoStruct;
			}
		}
		LOGGER.warn("根据custNo查询促销会员信息为空;custNo：{},返回的对象为：{}", custNo, infoResp);
		return null;
	}

	/**
	 * 转换客户意向品类品牌 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cates
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private List<MemCustomerIntentCateVo> convertMemCustomerIntentCates(List<MemCustomerIntentCate> cates) {
		if (CollectionUtils.isNotEmpty(cates)) {
			List<MemCustomerIntentCateVo> vos = new ArrayList<MemCustomerIntentCateVo>();
			MemCustomerIntentCateVo vo;
			for (MemCustomerIntentCate cate : cates) {
				if (null != cate) {
					vo = new MemCustomerIntentCateVo();
					vo.setCateCode(cate.getCateCode());
					vo.setCateName(cate.getCateName());
					vo.setBrands(cate.getBrands());
					vos.add(vo);
				}
			}
			return vos;
		}
		return Collections.emptyList();
	}

	/**
	 * 转换客户意向商品 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param cmmdtys
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private List<MemCustomerIntentCmmdtyVo> convertMemCustomerIntentCmmdtys(List<MemCustomerIntentCmmdty> cmmdtys) {
		if (CollectionUtils.isNotEmpty(cmmdtys)) {
			List<MemCustomerIntentCmmdtyVo> vos = new ArrayList<MemCustomerIntentCmmdtyVo>();
			MemCustomerIntentCmmdtyVo vo;
			for (MemCustomerIntentCmmdty cmmdty : cmmdtys) {
				if (null != cmmdty) {
					vo = new MemCustomerIntentCmmdtyVo();
					vo.setCmmdtyCode(cmmdty.getCmmdtyCode());
					vo.setCmmdtyName(cmmdty.getCmmdtyName());
					vo.setPrice(cmmdty.getPrice());
					vos.add(vo);
				}
			}
			return vos;
		}
		return Collections.emptyList();
	}

	/**
	 * 保存跟进记录
	 */
	@Override
	public ReturnMsg<String> saveCustomerFollow(String storeCode, String staffId, Long custId, int type, String nextTime, String memo, String result, String devNo, String ip) {
		LOGGER.info("saveCustomerFollow,{},{},{},{},{},{},{}", storeCode, staffId, custId, type, nextTime, memo, result);
		ReturnMsg<String> returnMsg = new ReturnMsg<String>();
		MemCustomer customer = memCustomerDao.getMemCustomer(custId);
		if (null != customer) {
			MemFollowLog log = new MemFollowLog();
			// 时间格式转换 yyyy-MM-dd
			if (StringUtils.isNotBlank(nextTime)) {
				log.setNextCallDate(DateUtils.getDateByStr(nextTime));
			}
			log.setCreateTime(new Date());
			log.setCustomerId(custId);
			log.setEmployeeId(customer.getEmployeeId());
			log.setMemo(memo);
			log.setResult(result);
			log.setStoreCode(customer.getStoreCode());
			log.setType(type);
			boolean successFlag = memCustomerDao.saveMemFollowLog(log);
			LOGGER.info("saveCustomerFollow,successFlag={}", successFlag);
			if (!successFlag) {
				// 保存失败
				returnMsg.setError(ErrorCodeConstants.ERR_MEM_0018);
			}

			// 接入风控
			submitSensitiveVisitRecord(devNo, customer.getMobile(), staffId, ip);
		} else {
			// 客户信息不存在
			returnMsg.setError(ErrorCodeConstants.ERR_MEM_0019);
		}
		LOGGER.info("saveCustomerFollow,response={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 查询客户跟进记录 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param staffId
	 * @param custId
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public ReturnMsg<MemFollowLogResponse> queryCustomerFollowList(String storeCode, String staffId, Long custId, int currIndex, int size) {
		LOGGER.info("queryCustomerFollowList,{},{},{},{},{}", storeCode, staffId, custId, currIndex, size);
		ReturnMsg<MemFollowLogResponse> returnMsg = new ReturnMsg<MemFollowLogResponse>();
		// 查询根据记录
		List<MemFollowLog> logs = memCustomerDao.queryCustomerFollowList(storeCode, staffId, custId, currIndex, size);
		if (CollectionUtils.isNotEmpty(logs)) {
			MemFollowLogResponse resp = new MemFollowLogResponse();
			// 转换跟进记录对象
			List<MemFollowLogVo> list = convertFollowLogs(logs);
			resp.setList(list);
			returnMsg.setData(resp);
		}
		LOGGER.info("queryCustomerFollowList,response={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 转换跟进记录对象 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param logs
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private List<MemFollowLogVo> convertFollowLogs(List<MemFollowLog> logs) {
		List<MemFollowLogVo> vos = new ArrayList<MemFollowLogVo>();
		MemFollowLogVo vo;
		for (MemFollowLog log : logs) {
			if (null != log) {
				vo = new MemFollowLogVo();
				vo.setType(log.getType());
				vo.setResult(log.getResult());
				vo.setMemo(log.getMemo());
				// 时间格式转换
				if (null != log.getNextCallDate()) {
					vo.setNextCallDate(DateUtils.getDateStrByFormat(log.getNextCallDate(), "yyyy-MM-dd"));
				}
				vo.setCreateTime(DateUtils.getDateStrByFormat(log.getCreateTime(), "yyyy-MM-dd"));
				vos.add(vo);
			}
		}
		return vos;
	}

	/**
	 * 查询共享客户列表 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param storeCode
	 * @param currIndex
	 * @param size
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@Override
	public ReturnMsg<MemCustomerResponse> queryShareCustomerList(String storeCode, String employeeName, int currIndex, int size) {
		LOGGER.info("queryShareCustomerList,{},{},{}", storeCode, currIndex, size);
		ReturnMsg<MemCustomerResponse> returnMsg = new ReturnMsg<MemCustomerResponse>();
		List<MemCustomer> custs = memCustomerDao.queryShareCustomerList(storeCode, employeeName, currIndex, size);
		if (CollectionUtils.isNotEmpty(custs)) {
			MemCustomerResponse resp = new MemCustomerResponse();
			// 转换共享客户
			List<MemCustomerVo> list = convertShareCustomer(custs);
			resp.setList(list);
			returnMsg.setData(resp);
		}
		LOGGER.info("queryShareCustomerList,response={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 转换共享客户 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param customers
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private List<MemCustomerVo> convertShareCustomer(List<MemCustomer> customers) {
		List<MemCustomerVo> custs = new ArrayList<MemCustomerVo>();
		MemCustomerVo vo;
		List<String> custList = new ArrayList<String>();
		// 处理客户编码,方便后面查询客户购物状态
		for (MemCustomer cust : customers) {
			if (null != cust && StringUtils.isNotBlank(cust.getCustNo())) {
				custList.add(cust.getCustNo());
			}
		}
		// 查询会员首购复购接口
		Map<String, String> frequencyList = shopFrequencyService.queryShopFrequency(custList);
		// 处理每个客户信息
		for (MemCustomer cust : customers) {
			// 数据不为空
			if (null != cust) {
				vo = new MemCustomerVo();
				vo.setCustId(cust.getId());
				vo.setCustName(StringUtils.isBlank(cust.getName()) ? "顾客" : cust.getName());
				// 员工ID,名称
				vo.setStaffId(cust.getEmployeeId());
				vo.setStaffName(cust.getEmployeeName());
				// 加密手机号
				if (StringUtils.isNotBlank(cust.getMobile())) {
					vo.setMobile(AESUtils.encrypt(cust.getMobile(), ConfigConstants.AES_KEY));
				} else {
					vo.setMobile("");
				}
				// 预计购买金额
				if (null != cust.getExpectPay()) {
					vo.setExpectPay(cust.getExpectPay().toString());
				} else {
					vo.setExpectPay("0.00");
				}
				// 日期转换
				if (null != cust.getExpectDate()) {
					vo.setExpectDate(DateUtils.getDateStrByFormat(cust.getExpectDate(), "yyyy-MM-dd"));
				} else {
					vo.setExpectDate("");
				}
				if (null != cust.getCreateTime()) {
					vo.setCreateTime(DateUtils.getDateStrByFormat(cust.getCreateTime(), "yyyy-MM-dd"));
				} else {
					vo.setCreateTime("");
				}
				// 设置首购，复购，未购提示信息
				vo.setBuyStatus(processBuyStatus(cust.getCustNo(), frequencyList));
				// 查询标签列表
				List<CustLabelVo> custLabels = labelService.queryFiveLabelByCustId(cust.getId());
				vo.setCustLabels(custLabels);
				custs.add(vo);
			}
		}
		return custs;
	}

	/**
	 * 处理会员首复购状态 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param custNo
	 * @param frequencyList
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private String processBuyStatus(String custNo, Map<String, String> frequencyList) {
		if (StringUtils.isBlank(custNo) || frequencyList.isEmpty()) {
			return CustshopFrequency.DealType.NOT_MEMBER;
		} else {
			// 获取客户购物状态对象
			CustshopFrequency custshopFrequency = DJStringUtils.getDealType(frequencyList.get(custNo));
			return custshopFrequency.getDealType();
		}
	}

	/**
	 * 分配共享客户信息
	 */
	@Override
	public ReturnMsg<String> updateShareCustomer(String storeCode, Long custId, String staffId) {
		LOGGER.info("updateShareCustomer,{},{},{}", storeCode, custId, staffId);
		ReturnMsg<String> returnMsg = new ReturnMsg<String>();
		// 查询该共享客户
		MemCustomer shareCust = memCustomerDao.getShareCustomer(storeCode, custId);
		if (null != shareCust) {
			// 核实分配人信息
			StoreManDto man = storeManService.queryStoreManAttachInfo(staffId);
			// 人员信息存在，且门店相同
			if (null != man && null != storeCode && storeCode.equals(man.getStoreCode())) {
				// 判断该店员是否已发展改会员
				MemCustomer hasCust = memCustomerDao.getMyCustomer(storeCode, staffId, shareCust.getCustNo(), shareCust.getMobile());
				if (null != hasCust) {
					// 员工信息不匹配无法分配
					returnMsg.setError(ErrorCodeConstants.ERR_MEM_0023);
					return returnMsg;
				}
				// 判断是否更新完成
				boolean succFlag = memCustomerDao.updateShareCustomer(custId, staffId, man.getStaffName());
				LOGGER.info("updateShareCustomer,updateShareCustomer={},custId={},staffId={}", succFlag, custId, staffId);
				// 更新成功, 更新客户详情表
				if (!succFlag) {
					// 更新失败
					returnMsg.setError(ErrorCodeConstants.ERR_MEM_0015);
				}
				if (null != shareCust.getMemberDetailId() && shareCust.getMemberDetailId() > 0) {
					succFlag = memCustomerDao.updateCustomerDetail(shareCust.getMemberDetailId(), staffId, man.getStaffName());
					LOGGER.info("updateShareCustomer,updateCustomerDetail={}", succFlag);
				}
			} else {
				// 员工信息不匹配无法分配
				returnMsg.setError(ErrorCodeConstants.ERR_MEM_0016);
			}
		} else {
			// 共享客户不存在,无法分配
			returnMsg.setError(ErrorCodeConstants.ERR_MEM_0017);
		}
		LOGGER.info("updateShareCustomer,resp={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 查询潜客列表
	 */
	@Override
	public ReturnMsg<MemCustomerResponse> queryPotentialCustomerList(String storeCode, String staffId, int currIndex, int size) {
		LOGGER.info("queryPotentialCustomerList,{},{},{},{}", storeCode, staffId, currIndex, size);
		ReturnMsg<MemCustomerResponse> returnMsg = new ReturnMsg<MemCustomerResponse>();
		// 查询潜客列表
		List<MemCustomer> list = memCustomerDao.queryPotentialCustomerList(storeCode, staffId, currIndex, size);
		if (CollectionUtils.isNotEmpty(list)) {
			// 转化潜客数据
			List<MemCustomerVo> custs = convertMemCustomers(list);
			MemCustomerResponse resp = new MemCustomerResponse();
			resp.setList(custs);
			returnMsg.setData(resp);
		}
		LOGGER.info("queryPotentialCustomerList,resp={}", returnMsg);
		return returnMsg;
	}

	/**
	 * 调用风控接口 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param devNo
	 * @param mobileNum
	 * @param staffId
	 * @param requestIp
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	private void submitSensitiveVisitRecord(String devNo, String mobileNum, String staffId, String requestIp) {
		KafkaProducer KafkaProducer = new KafkaProducer(kafkaZkServer, topic);
		JSONObject jb = new JSONObject();
		jb.put("employee_id", staffId);
		jb.put("sys_name", "SNSAWP");
		jb.put("ip_addr", requestIp);
		// 设备号
		if (StringUtils.isNotBlank(devNo)) {
			jb.put("mac_addr", AESUtils.decrypt(devNo, ConfigConstants.AES_KEY));
		}
		jb.put("visit_page", "会员拓展跟进拨号");
		jb.put("visit_time", DateUtils.getDateStrByFormat(new Date(), DateUtils.ACCURATE_MINUTES_PATTERN_2));
		jb.put("rece_tel", "Y");
		try {
			// Member Expand会员拓展
			KafkaProducer.produce(jb.toString(), "SNSAWP_ME");
		} catch (Exception e) {
			LOGGER.error("me:submitSensitiveVisitRecord exception " + jb.toString(), e);
		}
	}

	/**
	 * 插入任务卡片消息提醒
	 */
	@Override
	public void dealPotentialCustomer() {
		LOGGER.info("enter dealPotentialCustomer");
		int cnt = memCustomerDao.dealPotentialCustomer();
		LOGGER.info("dealPotentialCustomer exit, insert cnt={}", cnt);
	}

	/**
	 * 更改客户为共享客户
	 */
	@Override
	public void updateToShareCustomer(String staffId) {
		LOGGER.info("enter updateToShareCustomer, staffId={}", staffId);
		int cnt = memCustomerDao.updateToShareCustomer(staffId);
		LOGGER.info("updateToShareCustomer exit, staffId={}, cnt={}", staffId, cnt);
	}

}
